package com.example.fengshoutong.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fengshoutong.MainApplication;
import com.example.fengshoutong.R;
import com.example.fengshoutong.adapter.SchoolContentAdapter;
import com.example.fengshoutong.adapter.SchoolTitleAdapter;
import com.example.fengshoutong.bean.Article;
import com.example.fengshoutong.greendao.ArticleDao;
import com.example.fengshoutong.utils.Constant;
import com.example.fengshoutong.utils.FragmentUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 农技学堂页面
 */
public class SchoolFragment extends Fragment {
    // 标题列表
    private final List<String> titleList = new ArrayList<>();
    private final List<Article> allArticles = new ArrayList<>();
    // 文章列表
    private final List<Article> searchArticles = new ArrayList<>();
    // 标题滑动视图
    private RecyclerView school_title_recycler_view;
    // 文章滑动视图
    private RecyclerView school_content_recycler_view;
    // 文章适配器
    private SchoolContentAdapter schoolContentAdapter;
    // 文章数据库
    private ArticleDao articleDao;
    private int selectType;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_school, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取文章数据库
        articleDao = MainApplication.getInstance().getGoodDaoSession().getArticleDao();
        initView();
    }

    private void initView() {
        allArticles.clear();
        searchArticles.clear();
        allArticles.addAll(articleDao.loadAll());
        selectType = 0;
        for (Article article : allArticles) {
            if (article.getType() == selectType) {
                searchArticles.add(article);
            }
        }
        school_title_recycler_view = getView().findViewById(R.id.school_title_recycler_view);
        school_content_recycler_view = getView().findViewById(R.id.school_content_recycler_view);
        titleList.addAll(Arrays.asList(Constant.titles));
        // 标题是横向一列滑动
        LinearLayoutManager titleLayout = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
        school_title_recycler_view.setLayoutManager(titleLayout);
        SchoolTitleAdapter schoolTitleAdapter = new SchoolTitleAdapter(titleList);
        school_title_recycler_view.setAdapter(schoolTitleAdapter);
        // 文章是纵向一列滑动
        LinearLayoutManager contentLayout = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        school_content_recycler_view.setLayoutManager(contentLayout);
        schoolContentAdapter = new SchoolContentAdapter(searchArticles);
        school_content_recycler_view.setAdapter(schoolContentAdapter);
        getView().findViewById(R.id.image_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentUtils.getInstance().switchFragment(R.id.nav_home);
            }
        });
        schoolTitleAdapter.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView text_title = v.findViewById(R.id.text_title);
                String title = text_title.getText().toString();
                for (int i = 0; i < Constant.titles.length; i++) {
                    if (title.equals(Constant.titles[i])) {
                        selectType = i;
                        break;
                    }
                }
                searchArticles.clear();
                for (Article article : allArticles) {
                    if (article.getType() == selectType) {
                        searchArticles.add(article);
                    }
                }
                schoolContentAdapter.notifyDataSetChanged();
            }
        });
    }


}
